
const Service = require('egg').Service;
const jwt = require('jsonwebtoken');

class UsersService extends Service {
  async login({ username, password }) { // 登陆注册一起用
    console.log('参数：===', username, password)
    const sql = `select username, password, userid from logininfo where username=? and password=?`;
    const data = await this.ctx.app.mysql.query(sql, [username, this.ctx.helper.getPassword(password)])
    if (data.length) {
      const token = jwt.sign({
        username,
        password,
        userid: data.userid
      }, this.ctx.app.config.keys, { expiresIn: '24h' })
      return { token, code: 0, msg: 'success' };
    } else {
      const sql = `insert into logininfo (username, password) values (?,?)`;
      const data = await this.ctx.app.mysql.query(sql, [username, this.ctx.helper.getPassword(password)])
      return { id: data.insertId, code: 0, msg: 'success' };
    }
  }
}

module.exports = UsersService